class_name ScoreIndicator
extends Label


## 分数更新的持续时间（毫秒）
@export var duration_score_update: int = 300


var current_score: int

var _old_score: int
var _updating_score: int
var _time_score_update: int


func _ready():
	current_score = 0
	display_score(current_score)



func _process(_delta: float):
	update_score()


# 分数递增
# 如果连击数为 5，则增加的分数为 5 + 4 +3 + 2 + 1 = 15
func add_combo(points: int):
	set_score(current_score + int((points * (points + 1)) / 2.0))


# 直接设置指定的分数
func set_score(points: int):
	_updating_score = 0
	_old_score = current_score
	current_score = max(0, points)
	_time_score_update = Time.get_ticks_msec()


func update_score():
	if current_score == _updating_score:
		return
	var progress := float(Time.get_ticks_msec() - _time_score_update) / duration_score_update
	if progress < 1:
		_updating_score = lerp(_old_score, current_score, progress)
	else:
		_updating_score = current_score
	display_score(_updating_score)



func display_score(socre: int):
	text = str(socre)